﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;

class Program
{
    enum Gender
    {
        Male,
        Female
    }
    const int COUNT = 10000;
    const string FILENAME = "D:\\data.txt";

    /// <summary>  
    /// 创建数据集并处理  
    /// </summary>  
    /// <param name="args"></param>  
    static void Main(string[] args)
    {
        // 创建随机数生成器  
        Random random = new Random();

        // 年龄数据  
        int mean = 45; // 正态分布的均值  
        int stdDev = 5; // 正态分布的标准差  
        List<int> Age = new List<int>();

        // 生成正态分布的年龄数据  
        for (int i = 0; i < COUNT; i++)
        {
            double u1 = random.NextDouble(); // [0,1) 范围内的均匀分布随机数  
            double u2 = random.NextDouble(); // [0,1) 范围内的均匀分布随机数  

            // Box-Muller变换  
            double z0 = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Cos(2.0 * Math.PI * u2);

            // 调整均值和标准差  
            double normalSample = mean + z0 * stdDev;

            Age.Add((int)normalSample);
        }
        for (int i = 0; i < COUNT; i++)
        {
            if (Age[i] < 0)
            { Age[i] = 0; }
            if (Age[i] > 100)
            { Age[i] = 100; }
        }
        // 名字数据  
        const int MINLENGTH = 3; // 最少字符数  
        const int MAXLENGTH = 5; // 最多字符数  
        const string uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        const string lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
        List<string> names = new List<string>();

        // 生成名字数据  
        for (int i = 0; i < COUNT; i++)
        {
            int length = random.Next(MINLENGTH, MAXLENGTH + 1);
            StringBuilder word = new StringBuilder(length);

            // 第一个字母总是大写  
            word.Append(uppercaseLetters[random.Next(uppercaseLetters.Length)]);

            // 后面的字母是小写  
            for (int j = 1; j < length; j++)
            {
                word.Append(lowercaseLetters[random.Next(lowercaseLetters.Length)]);
            }

            names.Add(word.ToString());
        }
        List<string> birthYear = new List<string>();
        for (int i = 0; i < COUNT; i++)
        {
            double Year = (double)DateTime.Today.Year - Age[i];
            birthYear.Add(Year.ToString());
        }


        List<string> dates = new List<string>();

        for (int i = 0; i < COUNT; i++)
        {
            string date = GenerateRandomMonthDay(random);
            dates.Add(date);
        }
        static string GenerateRandomMonthDay(Random random)
        {
            int month = random.Next(1, 13); // 生成1到12的随机月份  
            int day;

            // 根据月份生成对应的天数范围  
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    day = random.Next(1, 32); // 31天  
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    day = random.Next(1, 31); // 30天  
                    break;
                case 2:
                    // 2月有28或29天（这里不考虑闰年，所以固定28天）  
                    day = random.Next(1, 29);
                    break;
                default:
                    throw new InvalidOperationException("Invalid month generated.");
            }

            // 格式化为MM-DD字符串  
            return month.ToString("D2") + "-" + day.ToString("D2");
        }
        List<string> fullDates = new List<string>();
        for (int i = 0; i < COUNT; i++)
        {
            string left = birthYear[i];
            string right = dates[i];

            // 左右合并字符串  
            string merged = left + "-" + right;
            fullDates.Add(merged);
        }
        //身高
        List<string> heights = new List<string>();
        for (int i = 0; i < COUNT; i++)
        {
            // 生成0.0到1.0之间的随机数  
            double scaledRandom = random.NextDouble();

            // 缩放到1.6到2.0的范围  
            double result = scaledRandom * (2.0 - 1.6) + 1.6;

            // 添加到列表中  
            heights.Add(result.ToString("F1"));
        }
        //性别
        List<Gender> genders = new List<Gender>();

        for (int i = 0; i < COUNT; i++)
        {
            // 随机生成0或1，然后转换为Gender枚举  
            Gender gender = random.Next(0, 2) == 0 ? Gender.Male : Gender.Female;

            // 添加到列表中  
            genders.Add(gender);
        }
        //号码
        List<string>number = new List<string>();
        for(int i=1; i < COUNT+1; i++)
        {
            number.Add(i.ToString());
        }
        // 将数据写入文件  
        using (StreamWriter writer = new StreamWriter(FILENAME))
        {
            for (int i = 0; i < COUNT; i++)
            {
                writer.WriteLine($"{number[i]},{names[i]}, {Age[i]}，{fullDates[i]},{heights[i]},{genders[i]}");
            }
        }
       
        // 打印完成信息
        Console.WriteLine("数据生成完毕，并已写入文件。");
        //转换类型
        List<double> Height= new List<double>();
        for (int i = 0; i < COUNT; i++)
        {

            Height.Add(double.Parse(heights[i]));
        }
        // 使用linq进行分组并计算平均值  
        var groupedAverages = genders.Zip(Height, (g, h) => new { Gender = g, Height = h })
           .GroupBy(item => item.Gender)
           .ToDictionary(
               g => g.Key,
               g => g.Average(item => item.Height)
           );

        // 输出结果  
        foreach (var kv in groupedAverages)
        {
            Console.WriteLine($"Key: {kv.Key}, Average: {kv.Value}");
        }
        
    }
}